package com.example.natatorium_management.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.natatorium_management.entity.Order;
import com.example.natatorium_management.entity.Reserve;
import com.example.natatorium_management.mapper.ReserveMapper;
import com.example.natatorium_management.result.restful.R;
import com.example.natatorium_management.result.restful.ResponseEunm;
import com.example.natatorium_management.service.OrderService;
import com.example.natatorium_management.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReserveServiceImpl extends ServiceImpl<ReserveMapper, Reserve> implements ReserveService {
    @Autowired
    ReserveMapper reserveMapper;

    @Autowired
    OrderService orderService;

    @Override
    public R<IPage<Reserve>> queryPage(int current, int size) {
        Page<Reserve> page = super.page(Page.of(current, size));
        super.page(page);
        return new R<>(ResponseEunm.SUCCESS,page);
    }

    @Override
    public R<Reserve> addReserve(Reserve reserve) {
        if(save(reserve)){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.ADD_FAIL,null);
    }

    @Override
    public R<Reserve> deleteReserveById(int reserveId) {
        if(removeById(reserveId)){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.DELETE_FAIL,null);
    }

    @Override
    public R<Reserve> findReserveById(int reserveId) {
        return new R<>(ResponseEunm.SUCCESS,getById(reserveId));
    }

    @Override
    public R<IPage<Reserve>> findReserve(Reserve reserve,int current,int size) {
        if (reserve.getReserveName()!=null && reserve.getPhone() != null){
           return new R<>(ResponseEunm.SUCCESS,reserveMapper.findReserveByReserveNameAndPhone(Page.of(current,size),reserve));
        }
        if (reserve.getPhone()!=null){
            return new R<>(ResponseEunm.SUCCESS,reserveMapper.findReserveByPhone(Page.of(current,size),reserve.getPhone()));
        }
        if (reserve.getReserveName()!=null){
            return new R<>(ResponseEunm.SUCCESS,reserveMapper.findReserveByReserveName(Page.of(current,size),reserve.getReserveName()));
        }
        if (reserve.getMemberName()!=null){
            return new R<>(ResponseEunm.SUCCESS,reserveMapper.findReserveByMemberName(Page.of(current,size),reserve.getMemberName()));
        }
        return new R<>(ResponseEunm.SUCCESS,reserveMapper.findReserveByGymnasiumName(Page.of(current,size),reserve.getGymnasiumName()));
    }

    @Override
    public R<Reserve> updateReserve(Reserve reserve) {
        if (reserve.getState()==1){
            Order order =new Order();
            order.setCustomerName(reserve.getReserveName());
            order.setGymnasiumName(reserve.getGymnasiumName());
            order.setPeoplenum(reserve.getPeoplenum());
            order.setCoachName(reserve.getCoachName());
            order.setTime(reserve.getShichang());
            order.setMemberName(reserve.getMemberName());
            order.setRemarks(reserve.getRemarks());
            order.setPrice(null);
            order.setPhone(reserve.getPhone());
            order.setState(0);
            System.out.println(order);
            orderService.addOrder(order);
        }
        if (reserveMapper.updateReserve(reserve)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.UPDATE_FAIL,null);
    }
}
